package com.sebray.journal;

import java.util.Calendar;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Journal extends MIDlet {

	private JournalFile file;

	public Journal() {
		file = new JournalFile();
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		String date = getCurrentDate();
		TextBox2 textBox = new TextBox2("New entry", date + "\n\n", 1000, TextField.ANY,
				this);

		setDisplay(textBox);
	}

	private String getCurrentDate() {

		Calendar calendar = Calendar.getInstance();

		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int monthNumber = calendar.get(Calendar.MONTH) + 1;
		int year = calendar.get(Calendar.YEAR) % 2000;
		
		return day + "/" + monthNumber + "/" + year;
	}

	private void setDisplay(Displayable aCanvas) {
		Display.getDisplay(this).setCurrent(aCanvas);
	}

	public void saveEntry(String text) {
		file.save(text);
	}
}
